﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace UdpSocketClientAsync {

    internal class UdpUserToken {

        private Socket _socket;
        internal Socket Socket {
            get {
                return _socket;
            }
            set {
                this._socket = value;
            }
        }

        private bool _socketClosed;
        internal bool SocketClosed {
            get {
                return _socketClosed;
            }
        }

        private AutoResetEvent _timeoutEvent;
        internal AutoResetEvent TimeoutEvent {
            get {
                return _timeoutEvent;
            }
        }

        private EndPoint _remoteEndPoint;
        internal EndPoint RemoteEndPoint {
            get {
                return _remoteEndPoint;
            }
        }

        private object _ticket;
        internal object Ticket {
            get {
                return _ticket;
            }
        }

        const bool InitialStateNonSignaled = false;

        internal UdpUserToken(EndPoint ep, Socket socket, object ticket) {

            this._remoteEndPoint = ep;
            this._socket = socket;
            this._ticket = ticket;
            this._timeoutEvent = new AutoResetEvent(InitialStateNonSignaled);

        }


        internal void CloseSocket() {

            this._socketClosed = true;

            this.Socket.Shutdown(SocketShutdown.Both);
            if (this.Socket.Connected) {
                this.Socket.Disconnect(true);
            }

            this.Socket.Close(0);
            this._socket = null;


        }
    }
}
